﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class Aircrafts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["ACL"].ToString().IndexOf("ND1") < 0)
            //{
            //    Response.Redirect("unauthorized.aspx");
            //}
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("ND2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you want to delete.";
                return;
            }

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_aircraft_delete";
                SqlParameter para = cmd.Parameters.AddWithValue("@tail_no", Request.Form["KEY"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Inserting failed!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("ND2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            //if (lstTaxTypes.SelectedValue == "?")
            //{
            //    lblError.Text = "Please select tax type!";
            //    return;
            //}
            int ret_code;
            txtTailNo.Text = txtTailNo.Text.TrimEnd().ToUpper();
            if (txtTailNo.Text.IndexOf('*') == 0)//返回0表示处于第一个字符
            {
                if (txtTailNo.Text.IndexOf("*" + Session["AIRLINE"].ToString().TrimEnd()) == 0)//返回0表示处于第一个字符
                {
                    ;
                }
                else
                {
                    lblError.Text = "Virtual aircraft must leaded by '*'+<Airline Code>!";
                    return;
                }
            }
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_aircraft_new";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@tail_no", txtTailNo.Text);
                para = cmd.Parameters.AddWithValue("@ac_type", lstAcTypes.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@surcharge_type", lstSurchargeTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Inserting failed!";
                else if (ret_code == 2)
                    lblError.Text = "Aircraft type not found!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnCabins_Click(object sender, EventArgs e)
        {
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a aircraft.";
                return;
            }
            Response.Redirect("AircraftCabins.aspx?tail_no=" + Request.Form["KEY"].ToString());
        }
        protected void btnSeats_Click(object sender, EventArgs e)
        {
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a aircraft.";
                return;
            }
            Response.Redirect("AircraftSeatMap.aspx?tail_no=" + Request.Form["KEY"].ToString());
        }
    }
}